package com.bt.lms.service.impl;

import com.bt.lms.entity.RegStuInfo;
import com.bt.lms.entity.RegStuMaterial;
import com.bt.lms.entity.page.Page;
import com.bt.lms.bo.PageConditionQueryApplyStudent;
import com.bt.lms.dao.servise.RegStuDao;
import com.bt.lms.dao.servise.impl.RegStuDaoImpl;
import com.bt.lms.service.RegStuInfoService;

import java.sql.*;
import java.util.UUID;


/**
 * 报名学员服务类
 *
 * @author 朱传博
 */

public class RegStuInfoServiceImpl implements RegStuInfoService {


    @Override
    public Page<RegStuInfo> pagingConditionQuery(Page<PageConditionQueryApplyStudent> page) throws SQLException {


        //sql语句
        String sql = "SELECT*FROM reg_stu limit ?,?";

        //调用
        RegStuDao regStuDao = new RegStuDaoImpl();
        Page<RegStuInfo> regStuInfoPage = regStuDao.pageQuery(sql, page);

        return regStuInfoPage;
    }

    @Override
    public boolean add(RegStuInfo regStuInfo) throws Exception {


        //首先要知道跟几张表有关系

        //1.有的属性不是用户输入的  需要维护属性

        UUID uuid = UUID.randomUUID();
        regStuInfo.setRegistrationCode(uuid.toString());//报名编号
        regStuInfo.setRegMaterialStatus(1);//报名材料状态
        regStuInfo.setEmailStatus(1);//邮箱状态
        regStuInfo.setAccountStatus(1);//帐号状态


        //sql语句
        String sql1 = "insert into reg_stu(req_code,name,gender,birthday,cred_type,cred_no,email,mobile_phone,req_way,qq,wechat)values(?,?,?,?,?,?,?,?,?,?,?)";


        RegStuDao regStuDao = new RegStuDaoImpl();
        regStuDao.insert(sql1, regStuInfo);


        //报名学员材料表


        String sql = "insert into reg_stu_material(reg_stu_id,material_type,url)values(?,?,?)";

        regStuDao.insert1(sql, regStuInfo);


        return true;
    }


    @Override
    public boolean edit(Long applyInfoID, RegStuInfo regStuInfo) throws ClassNotFoundException, SQLException {


        //维护不是输入的属性
        regStuInfo.setRegMaterialStatus(1);//报名材料状态
        regStuInfo.setEmailStatus(1);//邮箱状态
        regStuInfo.setAccountStatus(1);//帐号状态


        //sql语句
        String sql = "update reg_stu set name=?,gender=?,birthday=?,cred_type=?,cred_no=?,email=?,mobile_phone=?,req_way=?,qq=?,wechat=? where id=?";


        RegStuDao regStuDao = new RegStuDaoImpl();
        regStuDao.editRegStuInfo(sql, applyInfoID, regStuInfo);


        return true;
    }

    @Override
    public boolean delete(Long applyInfoID) throws SQLException, ClassNotFoundException {


        //sql 语句
        String sql = "update reg_stu set deleted =?where id= ?";

        RegStuDao regStuDao = new RegStuDaoImpl();
        regStuDao.deleteRegStuInfo(sql, applyInfoID);


        return true;
    }

    @Override
    public boolean enroll(Long applyInfoID) {


        return false;
    }

    @Override
    public RegStuMaterial queryMaterial(Long applyInfoID) throws SQLException, ClassNotFoundException {


        //报名材料信息
        String sql1 = "select material_type from reg_stu_material where reg_stu_id =?";
        //掉方法
        RegStuDao regStuDao = new RegStuDaoImpl();

        RegStuInfo regStuInfo = new RegStuInfo();
        regStuDao.selectStuMaterial(sql1, regStuInfo, applyInfoID);
        return null;
    }

    @Override
    public RegStuInfo queryDetail(Long applyInfoID) throws SQLException, ClassNotFoundException {


        //要执行的sql语句
        String sql = "SELECT*FROM reg_stu where id=?";
        //报名学员信息
        RegStuInfo regStuInfo = new RegStuInfo();

        //掉方法
        RegStuDao regStuDao = new RegStuDaoImpl();
        regStuDao.selectStu(sql, regStuInfo, applyInfoID);


        //招生计划
        Long enpID = regStuInfo.getEnpID();
        String sql3 = "select  name from enp_plan where id=?";

        //掉方法

        regStuDao.selectEnp(sql3, enpID, applyInfoID);


        return regStuInfo;
    }


}



